package com.luo.chengrui.module.crbpm.controller.admin.orgprocess;

import com.luo.chengrui.module.crbpm.service.flowable.ActReModelService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.crbpm.controller.admin.orgprocess.vo.*;
import com.luo.chengrui.module.crbpm.dal.dataobject.orgprocess.BpmOrgProcessDO;
import com.luo.chengrui.module.crbpm.convert.orgprocess.BpmOrgProcessConvert;
import com.luo.chengrui.module.crbpm.service.orgprocess.BpmOrgProcessService;

@Tag(name = "管理后台 - 组织模板明细")
@RestController
@RequestMapping("/crbpm/bpm-org-process")
@Validated
public class BpmOrgProcessController {

    @Resource
    private BpmOrgProcessService bpmOrgProcessService;
    @Resource
    private ActReModelService actReModelService;

    @PostMapping("/create")
    @Operation(summary = "创建组织模板明细")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-process:create')")
    public CommonResult<String> createBpmOrgProcess(@Valid @RequestBody BpmOrgProcessCreateReqVO createReqVO) {
        return success(bpmOrgProcessService.createBpmOrgProcess(createReqVO));
    }

    @PostMapping("/create/batch")
    @Operation(summary = "创建组织模板明细")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-process:create')")
    public CommonResult<Boolean> batchCreateBpmOrgProcess(@Valid @RequestBody List<BpmOrgProcessCreateReqVO> createReqVO) {
        bpmOrgProcessService.batchCreateBpmOrgProcess(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织模板明细")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-process:update')")
    public CommonResult<Boolean> updateBpmOrgProcess(@Valid @RequestBody BpmOrgProcessUpdateReqVO updateReqVO) {
        bpmOrgProcessService.updateBpmOrgProcess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织模板明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-process:delete')")
    public CommonResult<Boolean> deleteBpmOrgProcess(@RequestParam("id") String id) {
        bpmOrgProcessService.deleteBpmOrgProcess(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织模板明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-process:query')")
    public CommonResult<BpmOrgProcessRespVO> getBpmOrgProcess(@RequestParam("id") String id) {
        BpmOrgProcessDO bpmOrgProcess = bpmOrgProcessService.getBpmOrgProcess(id);
        return success(BpmOrgProcessConvert.INSTANCE.convert(bpmOrgProcess));
    }

    @GetMapping("/list")
    @Operation(summary = "获得组织模板明细列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-process:query')")
    public CommonResult<List<BpmOrgProcessRespVO>> getBpmOrgProcessList(@RequestParam("ids") Collection<String> ids) {
        List<BpmOrgProcessDO> list = bpmOrgProcessService.getBpmOrgProcessList(ids);
        return success(BpmOrgProcessConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织模板明细分页")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-process:query')")
    public CommonResult<PageResult<BpmOrgProcessRespVO>> getBpmOrgProcessPage(@Valid BpmOrgProcessPageReqVO pageVO) {
        PageResult<BpmOrgProcessDO> pageResult = bpmOrgProcessService.getBpmOrgProcessPage(pageVO);
        List<BpmOrgProcessRespVO> voList = bpmOrgProcessService.convertTo(pageResult.getList());
        PageResult<BpmOrgProcessRespVO> result = new PageResult<>();
        result.setList(voList);
        result.setTotal(pageResult.getTotal());
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织模板明细 Excel")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-process:export')")
    @OperateLog(type = EXPORT)
    public void exportBpmOrgProcessExcel(@Valid BpmOrgProcessExportReqVO exportReqVO,
                                         HttpServletResponse response) throws IOException {
        List<BpmOrgProcessDO> list = bpmOrgProcessService.getBpmOrgProcessList(exportReqVO);
        // 导出 Excel
        List<BpmOrgProcessExcelVO> datas = BpmOrgProcessConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "组织模板明细.xls", "数据", BpmOrgProcessExcelVO.class, datas);
    }

}
